package mlog

import (
	"github.com/astaxie/beego/logs"
	"os"
	"runtime"
	"strconv"
	"strings"
)

// RFC5424 log message levels.
const (
	LevelEmergency = iota
	LevelAlert
	LevelCritical
	LevelError
	LevelWarning
	LevelNotice
	LevelInformational
	LevelDebug
)

var Log *logs.BeeLogger

func Init(logsFileName string, level int) {
	Log = logs.NewLogger()
	Log.SetLevel(level)
	_ = Log.SetLogger(logs.AdapterConsole)
	_ = Log.SetLogger(logs.AdapterFile, `{"filename":"`+logsFileName+`","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10}`)
	Log.EnableFuncCallDepth(true)
	Log.SetPrefix("[" + strings.ToUpper(strings.TrimLeft(os.Args[0], "./")) + "]")

	//mlog.Log.Async(1e3)
}

func GetCallerPrefix() string {
	_, file, line, ok := runtime.Caller(2)
	if !ok {
		file = "???"
		line = 0
	}
	//_, filename := path.Split(file)
	return "[MSG: " + file + ":" + strconv.Itoa(line) + "] "
}